package com.hcb.micro.sdk;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class BankcardCheck {
	public static void main(String[] args) {
		check("潘奇","6228480329410095676");
	}
	public static boolean check(String name,String number){
		//连接超时及读取超时设置
		System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); //连接超时：30秒
		System.setProperty("sun.net.client.defaultReadTimeout", "30000");	//读取超时：30秒

		//新建一个StringBuffer链接
		StringBuffer buffer = new StringBuffer();
		
		try {
			
			//把发送链接存入buffer中，如连接超时，可能是您服务器不支持域名解析
			buffer.append("http://api.chinadatapay.com/financial/bank/1447?key=1efbd6de77b07af25a6dd05ccd4f633b&acct_pan="+number+"&acct_name="+name);
			
			//把buffer链接存入新建的URL中
			URL url = new URL(buffer.toString());

			//打开URL链接
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			
			//使用POST方式发送
			connection.setRequestMethod("GET");

			//使用长链接方式
			connection.setRequestProperty("Connection", "Keep-Alive");
			
			//发送短信内容
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			
			//获取返回值
			String result = reader.readLine();
			System.out.println(result);
			int index = result.indexOf("code");
			int index_last = result.indexOf(",");
			String resultNumber = result.substring(index+7, index_last-1);
			//输出result内容，查看返回值，成功为success，错误为error，详见该文档起始注释
			if("10000".equals(resultNumber)){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}
}
